﻿<%@ WebHandler Language="C#" Class="ClearImageHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing.Imaging;
using TDS.Captcha;

public class ClearImageHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            // Create a CAPTCHA image using the text stored in the Session object.
            if (context.Request.Params["ticket"] != null)
            {
                string _number = context.Request.Params["ticket"].ToString();
                CaptchaImage ci = new CaptchaImage(_number, true);
                // Change the response headers to output a JPEG image.
                context.Response.Clear();
                context.Response.ContentType = "image/jpeg";

                // Write the image to the response stream in JPEG format.
                ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

                // Dispose of the CAPTCHA image object.
                ci.Dispose();
            }
        }
        catch (Exception ex)
        {
            string _number = "Invalid Number";
            CaptchaImage ci = new CaptchaImage(_number, true);
            // Change the response headers to output a JPEG image.
            context.Response.Clear();
            context.Response.ContentType = "image/jpeg";

            // Write the image to the response stream in JPEG format.
            ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

            // Dispose of the CAPTCHA image object.
            ci.Dispose();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}